package principal;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import operadores.crossover.TipoOperadorCrossover;

public class TelaParametros extends javax.swing.JFrame {

    public TelaParametros() {
        initComponents();

        KeyAdapter ka = new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char vChar = e.getKeyChar();
                if (!(Character.isDigit(vChar)
                        || (vChar == KeyEvent.VK_BACK_SPACE)
                        || (vChar == KeyEvent.VK_DELETE))) {
                    e.consume();
                }
            }
        };

        jTextFieldTamanhoPopulacao.addKeyListener(ka);
        jTextFieldTaxaMutacao.addKeyListener(ka);
        jTextFieldTamanhoTorneio.addKeyListener(ka);
        jTextFieldMaxItSemMelhoria.addKeyListener(ka);
        jTextFieldLimiteTempo.addKeyListener(ka);

        jTextFieldTamanhoPopulacao.setText("" + AlgGeneticoCaixeiroViajante.getTamanhoDaPopulacao());
        jTextFieldTaxaMutacao.setText("" + AlgGeneticoCaixeiroViajante.getTaxaDeMutacao());
        jTextFieldTamanhoTorneio.setText("" + AlgGeneticoCaixeiroViajante.getTamanhoDoTorneio());
        jTextFieldMaxItSemMelhoria.setText("" + AlgGeneticoCaixeiroViajante.getMaxIteracoesSemMelhoria());
        jTextFieldLimiteTempo.setText("" + AlgGeneticoCaixeiroViajante.getLimiteDeTempo());
        jComboBoxOpCross.setSelectedItem(AlgGeneticoCaixeiroViajante.getTipoOpCross());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jFileChooser = new javax.swing.JFileChooser();
        jLabelArquivo = new javax.swing.JLabel();
        jLabelArquivo1 = new javax.swing.JLabel();
        jLabelArquivo2 = new javax.swing.JLabel();
        jLabelArquivo3 = new javax.swing.JLabel();
        jTextFieldTamanhoPopulacao = new javax.swing.JTextField();
        jTextFieldTaxaMutacao = new javax.swing.JTextField();
        jTextFieldMaxItSemMelhoria = new javax.swing.JTextField();
        jTextFieldTamanhoTorneio = new javax.swing.JTextField();
        jButtonSalvar = new javax.swing.JButton();
        jLabelArquivo4 = new javax.swing.JLabel();
        jLabelArquivo5 = new javax.swing.JLabel();
        jComboBoxOpCross = new javax.swing.JComboBox();
        jTextFieldLimiteTempo = new javax.swing.JTextField();

        jFileChooser.setDialogTitle("Selecione um arquivo de vértices no formato .tsp");
        jFileChooser.setFileFilter(new FileFilterTxt());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Parâmetros");
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabelArquivo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelArquivo.setText("Tamanho da população:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.insets = new java.awt.Insets(30, 30, 0, 0);
        getContentPane().add(jLabelArquivo, gridBagConstraints);

        jLabelArquivo1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelArquivo1.setText("Taxa de mutação (%):");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.insets = new java.awt.Insets(0, 30, 0, 0);
        getContentPane().add(jLabelArquivo1, gridBagConstraints);

        jLabelArquivo2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelArquivo2.setText("Máx. de iter. seguidas sem melhoria:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.insets = new java.awt.Insets(0, 30, 0, 0);
        getContentPane().add(jLabelArquivo2, gridBagConstraints);

        jLabelArquivo3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelArquivo3.setText("Limite de tempo (s):");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.insets = new java.awt.Insets(0, 30, 0, 0);
        getContentPane().add(jLabelArquivo3, gridBagConstraints);

        jTextFieldTamanhoPopulacao.setPreferredSize(new java.awt.Dimension(70, 28));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(30, 10, 0, 50);
        getContentPane().add(jTextFieldTamanhoPopulacao, gridBagConstraints);

        jTextFieldTaxaMutacao.setPreferredSize(new java.awt.Dimension(70, 28));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 50);
        getContentPane().add(jTextFieldTaxaMutacao, gridBagConstraints);

        jTextFieldMaxItSemMelhoria.setPreferredSize(new java.awt.Dimension(70, 28));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 50);
        getContentPane().add(jTextFieldMaxItSemMelhoria, gridBagConstraints);

        jTextFieldTamanhoTorneio.setPreferredSize(new java.awt.Dimension(70, 28));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 50);
        getContentPane().add(jTextFieldTamanhoTorneio, gridBagConstraints);

        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.insets = new java.awt.Insets(30, 0, 30, 0);
        getContentPane().add(jButtonSalvar, gridBagConstraints);

        jLabelArquivo4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelArquivo4.setText("Tamanho do torneio (para seleção):");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.insets = new java.awt.Insets(0, 30, 0, 0);
        getContentPane().add(jLabelArquivo4, gridBagConstraints);

        jLabelArquivo5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelArquivo5.setText("Operador de crossover:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.insets = new java.awt.Insets(0, 30, 0, 0);
        getContentPane().add(jLabelArquivo5, gridBagConstraints);

        jComboBoxOpCross.setModel(new javax.swing.DefaultComboBoxModel(TipoOperadorCrossover.values()));
        jComboBoxOpCross.setPreferredSize(new java.awt.Dimension(70, 27));
        jComboBoxOpCross.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxOpCrossActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 50);
        getContentPane().add(jComboBoxOpCross, gridBagConstraints);

        jTextFieldLimiteTempo.setPreferredSize(new java.awt.Dimension(70, 28));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 50);
        getContentPane().add(jTextFieldLimiteTempo, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarActionPerformed
        try {
            int tamanhoDaPopulacao = Integer.parseInt(jTextFieldTamanhoPopulacao.getText());
            int taxaDeMutacao = Integer.parseInt(jTextFieldTaxaMutacao.getText());
            int tamanhoDoTorneio = Integer.parseInt(jTextFieldTamanhoTorneio.getText());
            int maxIteracoesSemMelhoria = Integer.parseInt(jTextFieldMaxItSemMelhoria.getText());
            int limiteDeTempo = Integer.parseInt(jTextFieldLimiteTempo.getText());
            TipoOperadorCrossover tipoOpCross = TipoOperadorCrossover.values()[jComboBoxOpCross.getSelectedIndex()];
            
            AlgGeneticoCaixeiroViajante.atualizaParametros(tamanhoDaPopulacao,
                    taxaDeMutacao, tamanhoDoTorneio, maxIteracoesSemMelhoria,
                    limiteDeTempo, tipoOpCross);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_jButtonSalvarActionPerformed

    private void jComboBoxOpCrossActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxOpCrossActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxOpCrossActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JComboBox jComboBoxOpCross;
    private javax.swing.JFileChooser jFileChooser;
    private javax.swing.JLabel jLabelArquivo;
    private javax.swing.JLabel jLabelArquivo1;
    private javax.swing.JLabel jLabelArquivo2;
    private javax.swing.JLabel jLabelArquivo3;
    private javax.swing.JLabel jLabelArquivo4;
    private javax.swing.JLabel jLabelArquivo5;
    private javax.swing.JTextField jTextFieldLimiteTempo;
    private javax.swing.JTextField jTextFieldMaxItSemMelhoria;
    private javax.swing.JTextField jTextFieldTamanhoPopulacao;
    private javax.swing.JTextField jTextFieldTamanhoTorneio;
    private javax.swing.JTextField jTextFieldTaxaMutacao;
    // End of variables declaration//GEN-END:variables
}
